package com.weasel.http.builder;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dylan on 2015/10/8.
 */
public class HttpPostMethodBuilder extends AbstractHttpEntityMethodBaseBuilder<HttpPostMethodBuilder> {

    private HttpPost post = new HttpPost();

    public HttpPostMethodBuilder withFormParams(Map<String,String> params){

        List<NameValuePair> _params = new ArrayList<>();
        params.forEach((k,v)->{
            NameValuePair param = new BasicNameValuePair(k, v);
            _params.add(param);
        });
        HttpEntity entity = new UrlEncodedFormEntity(_params, Consts.UTF_8);
        withEntity(entity);
        return this;
    }

    @Override
    public HttpPost getMethod() {
        return post;
    }
}
